package com.example.animaldoctor03;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import zhzz.SingleDataofzhzz;
import commonClass.ADAQueryClinicalSymptomsKeyByDiseaseIDAction;
import commonClass.FunctionDispatcher;
import commonClass.RemoteActionsName;
import commonClass.FunctionDispatcher.IRequestCompeletListener;
import bingliandshuju.SingleDataofAll;
import bingliandshuju.SingleInformationofIllnessDetail;
import Modle.disease.ClinicalSymptomsKey;
import Modle.disease.Symptoms;
import Modle.disease.SymptomsCategorie;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import Modle.disease.ClinicalSymptomsKey;
public class ZhuanJiaZD extends Activity {

	// 让最开始时传来一个值等于1
	private String nameofZJZD = ""  ;
	private String  GLofZJZD = null;
	private ArrayList<Object> resultListofZJZD = null;
	private Button buttonofJBXQ = null  , buttonofzhuanjiazdsuoyouxuanze5 = null  ;
	private Button  buttonofzhuanjiazdtoagain = null ,buttonofZJF = null;
	private TextView  textviewofzhuanjiazd = null;
	private ListView listofzhuanjiazd = null ;
	
	private List<Object> listofZJZDgetinformation = new ArrayList<Object>();
	private Symptoms symptomsofZJZDgetinformation = null ;
	private String animalIDifZJZD = "";
	private void initElemofZJZD (){
		setContentView(R.layout.zhuanjiazd);
		buttonofZJF =(Button)findViewById(R.id.buttonofZJF);
		listofzhuanjiazd = (ListView)findViewById(R.id.listofzhuanjiazd);
		buttonofJBXQ = (Button)findViewById(R.id.buttonofzhuanjiazd1); //xiangqing
		buttonofzhuanjiazdsuoyouxuanze5 = (Button)findViewById(R.id.buttonofzhuanjiazd5); //suoyouxuanze
		buttonofzhuanjiazdtoagain = (Button)findViewById(R.id.buttonofzhuanjiazd2);
		textviewofzhuanjiazd = (TextView)findViewById(R.id.textviewofzhuanjiazd);
		
		buttonofJBXQ.setOnClickListener(new listenerofzhuanjiazdofJBXQ() );
		buttonofzhuanjiazdtoagain.setOnClickListener(new listenerofzhuanjiazdtoagain());
		buttonofZJF.setOnClickListener(new listenerofZJF());
	}
	
	private void getInformationFSA (){
		
		SingleDataofAll singleDataofZZJD = SingleDataofAll.getInstance();
		
		symptomsofZJZDgetinformation = singleDataofZZJD.getObject();
		
		listofZJZDgetinformation = singleDataofZZJD.getListinformation();
	}
	private void getInformationfromupofZJZD(){
		Intent intentofZJZD = getIntent();
		Bundle bundleofZJZD = intentofZJZD.getExtras();
		ClinicalSymptomsKey CofZJZD = (ClinicalSymptomsKey) bundleofZJZD.getSerializable("shuju");
		animalIDifZJZD = CofZJZD.getDiseaseID();
		nameofZJZD = CofZJZD.getDiseaseName();
		
		DecimalFormat formatofZZZD = new DecimalFormat("#.0000");
		GLofZJZD =formatofZZZD.format( CofZJZD.getDiseaseProbability()) ;
		
		Message msgofZZZD = new Message ();
		msgofZZZD.what = 1 ;
		handleofzhuanjiazd.sendMessage(msgofZZZD);
		
	}
	private ArrayList<Object> assemblyofZJZD() {

		getInformationfromupofZJZD();
		ArrayList<Object> list = new ArrayList<Object>();

		list.add(animalIDifZJZD);
		
		// Integer i = new Integer(10);
		// list.add(i);

		return list;
	}

private void bindingofZJZD(final ArrayList<Object> list){
	
	try{
	
	getInformationFSA();
	List<String> listofoldIds = new ArrayList<String>();
	if(symptomsofZJZDgetinformation != null){
	listofoldIds.add(symptomsofZJZDgetinformation.getSymptomsID());
	}
	for(int i = 0 ; i < listofZJZDgetinformation.size(); i ++ ){
		
		ClinicalSymptomsKey ctext = (ClinicalSymptomsKey) listofZJZDgetinformation.get(i);
		listofoldIds.add(ctext.getClinicalSymptomsKeyID());
		
	}
	List<Object> listofZZhuan = new ArrayList<Object>();
	List<Object> listofZZZDQC = new ArrayList<Object>();
	
	for(int j = 0 ; j < list.size() ; j ++){
		
		ClinicalSymptomsKey cetext1 = (ClinicalSymptomsKey) list.get(j);
		String sofctext1 = cetext1.getClinicalSymptomsKeyID();
		for( String s : listofoldIds){
			if(sofctext1.equals(s)){
			
				listofZZZDQC.add(cetext1);
		}
		
	}
	}
		
	for(int i = 0 ; i < list.size(); i++ ){
		

		int jishuofZZZD = 0;
		ClinicalSymptomsKey ctext2 = (ClinicalSymptomsKey) list.get(i);
		String sofctext2 = ctext2.getClinicalSymptomsKeyID();
		for(String s : listofoldIds){
			if(!sofctext2.equals(s)){
				jishuofZZZD ++;
				
			}
			if(jishuofZZZD== listofoldIds.size()){
				listofZZhuan.add(ctext2);
			}
			
		}
		
	
	}
	
		
		String[] resultofZJZD = new String[listofZZhuan.size()];
		// list.toArray(new String[list.size()]);
		for (int i = 0; i < listofZZhuan.size(); i++) {
			
			
			//haiyouwenti
			resultofZJZD[i] =( (ClinicalSymptomsKey)listofZZhuan.get(i)).getClinicalSymptomsKey() ;
		}
		
		
		System.out.println("************* of the first ye mian " + resultofZJZD.length);

		ArrayAdapter<String> arrayAdapterofZJZD = new  ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice ,resultofZJZD );
		listofzhuanjiazd.setAdapter(arrayAdapterofZJZD);
		buttonofzhuanjiazdsuoyouxuanze5.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 ArrayList<Object> listofnextofZJZD = new ArrayList <Object>();
					
					for(long ids :listofzhuanjiazd.getCheckItemIds() ){
						
					int i = (int) ids;
						
						listofnextofZJZD.add((ClinicalSymptomsKey)list.get(i));
						//System.out.println("#################" + ids);
					}
					
					//把数据储存好
					SingleDataofAll singleDataofAllofZJZD = SingleDataofAll.getInstance();
					singleDataofAllofZJZD.setListinformation(listofnextofZJZD);
					try{
						Intent intentofzhuanjiazdsuoyouxuanze = new Intent(ZhuanJiaZD.this , YiXuanZhengZhuang.class);
						startActivity(intentofzhuanjiazdsuoyouxuanze);
						finish();
						}catch(RuntimeException e){
							android.app.AlertDialog.Builder builderofZJZDoftoYXZZ = new AlertDialog.Builder(ZhuanJiaZD.this);
							builderofZJZDoftoYXZZ.setMessage("无法使用该功能 , 请重试或者使用其他功能");
							builderofZJZDoftoYXZZ.create().show();
						}
			}
		});
	}catch(NullPointerException e){
		android.app.AlertDialog.Builder builderofZJZD = new AlertDialog.Builder(ZhuanJiaZD.this);
		builderofZJZD.setMessage("无数据,请用其他功能");
		builderofZJZD.create().show();
	}
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	
		initElemofZJZD();
		ArrayList<Object> parasListofZJZD = assemblyofZJZD();
		
		final ArrayList<Object> resultListTofZJZD = null;
		
		ADAQueryClinicalSymptomsKeyByDiseaseIDAction iActionofZJZD = new ADAQueryClinicalSymptomsKeyByDiseaseIDAction();
		FunctionDispatcher functionDispacherofZJZD = new FunctionDispatcher();
		
		functionDispacherofZJZD.exceute(RemoteActionsName.QueryClinicalSymptomsKeyByDiseaseIDAction, parasListofZJZD, iActionofZJZD, new IRequestCompeletListener() {

			@Override
			public void doCompelet(ArrayList<Object> resultList) {
				// TODO Auto-generated method stub
				try{
				Log.v("ZhengZhuangFZ2", "post:" + resultList.toString());
				ZhuanJiaZD.this.resultListofZJZD = resultList;
				bindingofZJZD(resultList);
				}catch(NullPointerException e){
					android.app.AlertDialog.Builder builderofZJZDoff = new AlertDialog.Builder(ZhuanJiaZD.this);
					builderofZJZDoff.setMessage("返回值为空 , 请用其他功能");
					builderofZJZDoff.create().show();
				}
			}
		},this);
		 
	}
	
	class listenerofZJF implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try{
				
				SingleDataofAll sAll = SingleDataofAll.getInstance();
				sAll.removeAll();
				SingleInformationofIllnessDetail sInfor = SingleInformationofIllnessDetail.getInstance();
				sInfor.removeAll();
				SingleDataofzhzz sDofzh = SingleDataofzhzz.getInstance();
				sDofzh.removeAll();
				
			Intent intentofZJF = new Intent(ZhuanJiaZD.this, ZhengDuanFF.class);
			startActivity(intentofZJF);
			finish();
			}catch(RuntimeException e){
				android.app.AlertDialog.Builder builderofZJZDofF = new AlertDialog.Builder(ZhuanJiaZD.this);
				builderofZJZDofF.setMessage("无法使用该功能 , 请重试或者使用其他功能");
				builderofZJZDofF.create().show();
			}
		}
		
	}
	class listenerofzhuanjiazdtoagain implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			
			//有问题未解决
			try{
			List<Object> listofagainofZJZD = new ArrayList<Object>();
			SingleDataofAll singDataofallofZJZD = SingleDataofAll.getInstance();
			listofagainofZJZD = singDataofallofZJZD.getListinformation();
			Bundle bundleofagainofBL3 = new Bundle();
			bundleofagainofBL3.putSerializable("shuju", (Serializable) listofagainofZJZD);
			Intent intentofzhuanjiazdtoagain = new Intent (ZhuanJiaZD.this ,AllIllness.class );
			intentofzhuanjiazdtoagain.putExtras(bundleofagainofBL3);
			startActivity(intentofzhuanjiazdtoagain);
			finish();
			}catch(RuntimeException e){
				android.app.AlertDialog.Builder builderofZJZDoftoA = new AlertDialog.Builder(ZhuanJiaZD.this);
				builderofZJZDoftoA.setMessage("无法使用该功能 , 请重试或者使用其他功能");
				builderofZJZDoftoA.create().show();
			}
			
		}
		
	}
	
	class listenerofzhuanjiazdofJBXQ implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try{
			Bundle bundleofZJZD = new Bundle();
			bundleofZJZD.putString("shuju", animalIDifZJZD);
			Intent intentofzhuanjiazdJBXQ = new Intent(ZhuanJiaZD.this , JBXQ.class);
			intentofzhuanjiazdJBXQ.putExtras(bundleofZJZD);
			startActivity(intentofzhuanjiazdJBXQ);
			finish();
			}catch(RuntimeException e){
				android.app.AlertDialog.Builder builderofZJZDoftoJBXQ = new AlertDialog.Builder(ZhuanJiaZD.this);
				builderofZJZDoftoJBXQ.setMessage("无法使用该功能 , 请重试或者使用其他功能");
				builderofZJZDoftoJBXQ.create().show();
			}
			
		}
		
	}


	
	public Handler handleofzhuanjiazd = new Handler() {
		public void handleMessage(Message msg) {	
			
			if(msg.what == 1){
				
				textviewofzhuanjiazd.setText("病名 :" + nameofZJZD + " " + "概率 ：" + "0" + GLofZJZD);
			}
			
		}
	};
	
}
